package frog.lazy.framework.core.service;

import frog.lazy.framework.core.mybatis.BasePO;
import frog.lazy.framework.core.mybatis.MyMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.List;

@CacheConfig(cacheNames = "base_service")
public abstract class MyService<T extends BasePO, K extends MyService> {

    @Autowired
    protected MyMapper<T> myMapper;

    @Autowired
    protected K thisProxy;

//    public void setThisProxy(K thisProxy) {
//        this.thisProxy = thisProxy;
//    }

    public List<T> select(T record){
        return myMapper.select(record);
    }

    public List<T> selectAll() {
        return myMapper.selectAll();
    }

    @Cacheable(key = "targetClass.canonicalName + ':' + T(String).valueOf(#key)", unless = "null == #result")
    public T selectByPrimaryKey(Object key) {
        return myMapper.selectByPrimaryKey(key);
    }

    public int selectCount(T record) {
        return myMapper.selectCount(record);
    }

    public T selectOne(T record) {
        return myMapper.selectOne(record);
    }

    public int delete(T record) {
        return myMapper.delete(record);
    }

    @CacheEvict(key = "targetClass.canonicalName + ':' + T(String).valueOf(#key)")
    public int deleteByPrimaryKey(Object key) {
        return myMapper.deleteByPrimaryKey(key);
    }

    public int insert(T record) {
        return myMapper.insert(record);
    }

    public int insertList(List<? extends T> recordList) {
        return myMapper.insertList(recordList);
    }

    public int insertSelective(T record) {
        return myMapper.insertSelective(record);
    }

    public int insertUseGeneratedKeys(T record) {
        return myMapper.insertUseGeneratedKeys(record);
    }

    @CachePut(key = "targetClass.canonicalName + ':' +  T(String).valueOf(#record.id)")
    public T updateByPrimaryKey(T record) {
        myMapper.updateByPrimaryKey(record);
        return record;
    }

    @CachePut(key = "targetClass.canonicalName + ':' + T(String).valueOf(#record.id)")
    public T updateByPrimaryKeySelective(T record) {
        myMapper.updateByPrimaryKeySelective(record);
        return record;
    }

    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return myMapper.selectByRowBounds(record, rowBounds);
    }

    /**
     * 清除默认设置缓存
     * @param key
     */
    @CacheEvict(key = "targetClass.canonicalName + ':' + T(String).valueOf(#key)")
    public void cleanDefaultCache(Object key){
    }

    /**
     * 清除默认设置缓存
     * @param
     */
    @CachePut(key = "targetClass.canonicalName + ':' + T(String).valueOf(#record.id)")
    public T updateDefaultCache(T record) {
        return record;
    }

    /**
     * 根据ID 一条一条的删除
     * @param recordList
     */
    public void deleteList(List<? extends T> recordList) {
        for (T record : recordList) {
            thisProxy.deleteByPrimaryKey(record.getId());
        }
    }

    public void updateOrInsert(T record){
        if(null == record.getId()){
            thisProxy.insertSelective(record);
        }else {
            thisProxy.updateByPrimaryKeySelective(record);
        }
    }

    /**
     * 根据ID判断一条条的新增或者
     * @param recordList
     */
    public void updateOrInsertList(List<? extends T> recordList) {
        for(T record : recordList) {
            this.updateOrInsert(record);
        }
    }

    public List<T> selectByExample(Weekend<T> weekend) {
        return myMapper.selectByExample(weekend);
    }

    public List<T> selectByExample(Weekend<T> weekend, RowBounds rowBounds) {
        return myMapper.selectByExampleAndRowBounds(weekend, rowBounds);
    }
}
